package com.eveinfo.tools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.eveinfo.EveInfo;
import com.eveinfo.display.CharacterSelectionView;
import com.eveinfo.display.PlannersView;
import com.eveinfo.display.SkillsView;
import com.eveinfo.display.asset.AssetsByItemsView;
import com.eveinfo.display.asset.AssetsByStationsView;
import com.eveinfo.display.setting.SettingCacheView;
import com.eveinfo.display.setting.SettingCharacterView;
import com.eveinfo.display.setting.SettingDownloadView;
import com.eveinfo.display.setting.SettingView;
import com.eveinfo.display.setting.SettingViewView;
import com.eveinfo.display.setting.SettingDownloadView.CheckFile;

public class EveInfoMenu {

	public enum View {
		PILOTS, SHEET, WALLET, OPTIONS, SKILLS, CHAR_SKILLS, CERTIFICATES, PLANNER, PLANNERS, ATTRIBUTES, ASSETS, SETTING_CHARACTERS, SETTING_CACHE, SETTING_VIEW, SETTING_DOWNLOAD, SHIP;
	}

	public static final int MENU_OPTIONS = 1;
	public static final int MENU_WALLET = 2;
	public static final int MENU_SKILLS = 3;
	public static final int MENU_CHAR_SKILLS = 4;
	public static final int MENU_CERTIFICATES = 5;
	public static final int MENU_PLANNERS = 6;
	public static final int MENU_ATTRIBUTES = 7;
	public static final int MENU_ASSETS = 8;

	public static final String MENU_CHAR_SKILLS_NAME = "Skills";
	public static final String MENU_WALLET_NAME = "Wallet";
	public static final String MENU_CERTIFICATES_NAME = "Certificates";
	public static final String MENU_PLANNERS_NAME = "Planner Manager";
	public static final String MENU_ATTRIBUTES_NAME = "Attributes";
	public static final String MENU_ASSETS_NAME = "Assets";

	public static boolean displayView(final Context context, View view) {
		final Intent myIntent;
		switch (view) {
		case PILOTS:
			myIntent = new Intent(context, CharacterSelectionView.class);
			context.startActivity(myIntent);
			return true;
		case OPTIONS:
			myIntent = new Intent(context, SettingView.class);
			context.startActivity(myIntent);
			return true;
		case SETTING_CACHE:
			myIntent = new Intent(context, SettingCacheView.class);
			context.startActivity(myIntent);
			return true;
		case SETTING_CHARACTERS:
			myIntent = new Intent(context, SettingCharacterView.class);
			context.startActivity(myIntent);
			return true;
		case SETTING_VIEW:
			myIntent = new Intent(context, SettingViewView.class);
			context.startActivity(myIntent);
			return true;
		case SETTING_DOWNLOAD:
			myIntent = new Intent(context, SettingDownloadView.class);
			context.startActivity(myIntent);
			return true;
		case SKILLS:
			myIntent = new Intent(context, SkillsView.class);
			context.startActivity(myIntent);
			return true;
		case PLANNERS:
			myIntent = new Intent(context, PlannersView.class);
			context.startActivity(myIntent);
			return true;
		case ASSETS:

			final String[] items = { "By Station", "By items" };

			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					final String string = items[item];
					displayAssetsView(context, string);
				}
			});
			AlertDialog alert = builder.create();
			alert.show();

		default:
			return false;
		}
	}

	private static boolean displayAssetsView(final Context context, String activity) {
		final Intent myIntent;
		if (activity.equals("By Station")) {
			myIntent = new Intent(context, AssetsByStationsView.class);
		} else {
			myIntent = new Intent(context, AssetsByItemsView.class);
		}
		if (!EveInfo.getPreferences().isCheckDatabases()) {
			context.startActivity(myIntent);
			return true;
		}
		final CheckFile itemsCheckFile = SettingDownloadView.checkFile(SettingDownloadView.ITEMS_DB_NAME);
		final CheckFile picturesCheckFile = SettingDownloadView.checkFile(SettingDownloadView.PICTURES_DB_NAME);
		if (!(itemsCheckFile == CheckFile.SAME_FILE) || !(picturesCheckFile == CheckFile.SAME_FILE)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			if (itemsCheckFile == CheckFile.FILE_EXIST || picturesCheckFile == CheckFile.FILE_EXIST) {
				builder.setMessage("You have the database but can't reach " + SettingDownloadView.SERVER_ADDRESS
						+ " to check the files.\n\n" + "Are you sure you want to continue?");
			} else if (itemsCheckFile == CheckFile.NOT_SAME_FILE || picturesCheckFile == CheckFile.NOT_SAME_FILE) {
				builder.setMessage("Not the same databases than the ones on " + SettingDownloadView.SERVER_ADDRESS
						+ ".\n\n" + "Are you sure you want to continue?");
			} else {
				builder.setMessage("You don't have the needed databases for this feature.\n\n"
						+ "Are you sure you want to continue?");
			}
			builder.setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					context.startActivity(myIntent);
				}
			});
			builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					displayView(context, View.SETTING_DOWNLOAD);
				}
			});
			builder.setCancelable(false);
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		} else {
			context.startActivity(myIntent);
			return true;
		}
	}
}
